<!DOCTYPE html>
<html xmlns:v-bind="http://www.w3.org/1999/xhtml">
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>小力权限</title>
    <meta name="description" content="小力权限">
    <meta name="keywords" content="小力权限">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="shortcut icon" href="../img/favicon.ico">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/element-ui/lib/theme-chalk/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
    <!-- 引入组件库 -->
    <script type="text/javascript" src="../js/vue.js"></script>
    <script type="text/javascript" src="../plugins/element-ui/lib/index.js"></script>
    <script type="text/javascript" src="../js/jquery.min.js"></script>
    <script type="text/javascript" src="../js/axios-0.18.0.js"></script>
</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>系统设置
            <small>菜单管理</small>
        </h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>系统设置</el-breadcrumb-item>
            <el-breadcrumb-item>菜单管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">
            <div class="filter-container" @keyup.enter="search()">
                <el-input placeholder="菜单名称" v-model="pagination.queryString" style="width: 200px;"
                          class="filter-item"></el-input>
                <el-button @click="search()" class="dalfBut">查询</el-button>
                <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
            </div>
            <!--default-expand-all展开所有子表格-->
            <el-table size="small" :data="dataList"
                      row-key="id"
                      border
                      :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
                <!--序号 角色名 菜单名称 路径 图标  描述-->
                <el-table-column type="index" align="center" label="序号"></el-table-column>
                <el-table-column prop="name" label="菜单名称" align="center"></el-table-column>
                <el-table-column prop="linkUrl" label="访问路径" align="center"></el-table-column>
                <el-table-column prop="path" label="路由路径" align="center"></el-table-column>
                <el-table-column prop="priority" label="优先级" align="center"></el-table-column>
                <el-table-column prop="icon" label="图标" align="center">
                    <template slot-scope="scope">
                        <i class="fa" :class="scope.row.icon"></i>
                    </template>
                </el-table-column>
                <el-table-column prop="description" label="描述" align="center"></el-table-column>
                <el-table-column label="操作" align="center" width="300">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                        <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination-container">
                <el-pagination
                        class="pagiantion"
                        @size-change="handleSizeChange"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-sizes="[10, 20, 30, 40, 50]"
                        :page-size="pagination.pageSize"
                        layout="total, sizes, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>

            <!-- 新增标签弹层 -->
            <div class="add-form">
                <el-dialog title="新增菜单" :visible.sync="dialogFormVisible">
                    <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right"
                             label-width="100px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="菜单名称" prop="name">
                                    <el-input v-model="formData.name"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="访问路径">
                                    <el-input placeholder="example.html" v-model="formData.linkUrl"></el-input>
                                </el-form-item>
                            </el-col>

                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="父菜单">
                                    <el-select v-model="formData.parentMenuId" placeholder="默认: 无" @change="changePath">
                                        <el-option
                                                v-for="menu in parentMenu"
                                                :key="menu.id"
                                                :label="menu.name"
                                                :label="menu.path"
                                                :value="menu.id">
                                        </el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="父菜单id">
                                    <!--这个id会自己跟着左边的父菜单变化, 所以属性写readonly, 不需要=true, 不然页面会报错-->
                                    <el-input v-model="formData.parentMenuId" disabled></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <!-- <el-form-item label="路由路径" >
                                     <el-input v-model="formData.BanPath" disabled></el-input>
                                 </el-form-item>-->
                                <el-form-item label="路由路径">
                                    <el-col :span="10">
                                        <el-input v-model="formData.BanPath" disabled></el-input>
                                    </el-col>
                                    <el-col class="line" :span="2"></el-col>
                                    <el-col :span="12">
                                        <el-input placeholder="一级菜单: 1  二级菜单: /1-1" v-model="formData.path"></el-input>
                                    </el-col>
                                </el-form-item>
                            </el-col>
                        </el-row>

                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="优先级">
                                    <el-select v-model="formData.priority">
                                        <el-option label="1" value="1"></el-option>
                                        <el-option label="2" value="2"></el-option>
                                        <el-option label="3" value="3"></el-option>
                                        <el-option label="4" value="4"></el-option>
                                        <el-option label="5" value="5"></el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="图标">
                                    <el-input v-model="formData.icon"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="描述">
                                    <el-input v-model="formData.description" type="textarea"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="handleAdd()">确定</el-button>
                    </div>
                </el-dialog>
            </div>
            <!-- 编辑详细弹层 -->
            <div class="add-form">
                <el-dialog title="编辑菜单" :visible.sync="dialogFormVisible4Edit">
                    <el-form ref="formData" :model="formData" :rules="rules" label-position="right"
                             label-width="100px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="菜单名称" prop="name">
                                    <el-input v-model="formData.name"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="访问路径">
                                    <el-input placeholder="example.html" v-model="formData.linkUrl"></el-input>
                                </el-form-item>
                            </el-col>

                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="父菜单">
                                    <el-select v-model="formData.parentMenuId" placeholder="默认: 无">
                                        <el-option
                                                v-for="menu in parentMenu"
                                                :key="menu.id"
                                                :label="menu.name"
                                                :label="menu.path"
                                                :value="menu.id">
                                        </el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="父菜单id">
                                    <!--这个id会自己跟着左边的父菜单变化, 所以属性写readonly, 不需要=true, 不然页面会报错-->
                                    <el-input v-model="formData.parentMenuId" readonly></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <!--<el-form-item label="路由路径">
                                    <el-input placeholder="一级菜单: 1  二级菜单: /1-1" v-model="formData.path"></el-input>
                                </el-form-item>-->
                                <el-form-item label="路由路径">
                                    <el-col :span="10">
                                        <el-input v-model="formData.BanPath" disabled></el-input>
                                    </el-col>
                                    <el-col class="line" :span="2"></el-col>
                                    <el-col :span="12">
                                        <el-input placeholder="一级菜单: 1  二级菜单: /1-1" v-model="formData.path"></el-input>
                                    </el-col>
                                </el-form-item>
                            </el-col>

                        </el-row>

                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="优先级">
                                    <el-select v-model="formData.priority">
                                        <el-option label="1" value="1"></el-option>
                                        <el-option label="2" value="2"></el-option>
                                        <el-option label="3" value="3"></el-option>
                                        <el-option label="4" value="4"></el-option>
                                        <el-option label="5" value="5"></el-option>
                                        <el-option label="6" value="6"></el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="图标">
                                    <el-input v-model="formData.icon"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="描述">
                                    <el-input v-model="formData.description" type="textarea"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                        <el-button type="primary" @click="handleEdit()">确定</el-button>
                    </div>
                </el-dialog>
            </div>
        </div>
    </div>
</div>
</body>
<script type="text/javascript">
    let vue = new Vue({
        el: '#app',
        data: {
            pagination: {//分页相关模型数据
                currentPage: 1,//当前页码
                pageSize: 30,//每页显示的记录数
                total: 100,//总记录数
                queryString: null//查询条件
            },
            menuData: [],
            dataList: [],//当前页要展示的分页列表数据
            formData: {},//表单数据
            tableData: [],//详细数据
            parentMenu: [{
                id: "1",
                name: "管理"
            }, {
                id: "5",
                name: "管理"
            }, {
                id: "11",
                name: "管理"
            }],
            multipleSelection: [],//选中role数据
            menuId: "",//编辑菜单id
            dialogFormVisible: false,//增加表单是否可见
            dialogFormVisible4Edit: false,//增加表单是否可见
            dialogFormVisible4Detail: false,
            rules: {//校验规则
                name: [{required: true, message: '菜单名称为必填项', trigger: 'blur'}],
            },
            icons: [
                {
                    label: "用户1",
                    value: "fa fa-user-md",
                },
                {
                    label: "用户2",
                    value: "fa fa-user-circle",
                },
                {
                    label: "电话",
                    value: "fa fa-tty",
                },
                {
                    label: "监听器",
                    value: "fa fa-stethoscope",
                },
                {
                    label: "心",
                    value: "fa fa-heartbeat",
                },
                {
                    label: "星",
                    value: "el-icon-star-on",
                },
                {
                    label: "多标签",
                    value: "fa fa-tags",
                },
                {
                    label: "钻石",
                    value: "fa fa-diamond",
                },
                {
                    label: "图表",
                    value: "fa fa-bar-chart",
                },
                {
                    label: "多用户",
                    value: "fa fa-users",
                },
                {
                    label: "上传",
                    value: "el-icon-upload",
                },
                {
                    label: "设置",
                    value: "el-icon-setting",
                },
                {
                    label: "手机",
                    value: "el-icon-mobile-phone",
                },
                {
                    label: "电脑",
                    value: "fa fa-television",
                },
                {
                    label: "位置",
                    value: "el-icon-location",
                },
                {
                    label: "分享",
                    value: "el-icon-share",
                },
                {
                    label: "回收",
                    value: "el-icon-delete",
                },
                {
                    label: "信息",
                    value: "el-icon-news",
                },
                {
                    label: "消息",
                    value: "el-icon-message",
                },
                {
                    label: "消息2",
                    value: "fa fa-commenting-o",
                },
                {
                    label: "chrome",
                    value: "fa fa-chrome",
                },
                {
                    label: "QQ",
                    value: "fa fa-qq",
                },
                {
                    label: "微信",
                    value: "fa fa-weixin",
                },
                {
                    label: "apple",
                    value: "fa fa-apple",
                },
            ]
        },
        //钩子函数，VUE对象初始化完成后自动执行
        created() {
            this.findPage();
        },
        methods: {
            // 初始页currentPage、初始每页数据数pagesize和数据data
            handleSizeChange(size) {
                this.pagination.pageSize = size;
                this.findPage();
            },
            //切换页码
            handleCurrentChange(currentPage) {
                this.pagination.currentPage = currentPage;
                this.findPage();
            },
            //没有操作权限弹出框
            showMessage(r) {
                if (r == 'Error: Request failed with status code 403') {
                    //权限不足
                    this.$message.error('无访问权限');
                    return;
                } else {
                    this.$message.error('未知错误');
                    return;
                }
            },
            search() {
                this.pagination.currentPage = 1;
                this.findPage();
            },
            // 重置表单
            resetForm(formName) {
                this.formData = {};
                if (this.$refs[formName] !== undefined) {
                    this.$refs[formName].resetFields();
                }
            },
            //分页查询
            findPage() {
                axios.post("/menu/findByPage.do", {
                    currentPage: this.pagination.currentPage,
                    pageSize: this.pagination.pageSize,
                    queryString: this.pagination.queryString
                }).then((res) => {
                    this.dataList = res.data.rows;
                    this.pagination.total = res.data.total;
                }).catch((res) => {
                    this.showMessage(res);
                });
            },
            changePath(val) {
                //parentMenu-for里面的也是datas里面的值
                let obj = {};
                obj = this.parentMenu.find((item) => {
                    return item.id === val;
                });
                let getPath = obj.path;
                this.formData.BanPath = "/" + getPath + "-";
                return;
            },
            // 弹出添加窗口
            handleCreate() {
                this.resetForm("dataAddForm");
                this.dialogFormVisible = true;
                //还需要查询父菜单信息, 供用户选择
                axios.get("/menu/findAllParent.do").then(res => {
                    if (res.data.flag) {
                        this.parentMenu = res.data.data;
                    } else {
                        this.$message({message: res.data.message, type: "error"})
                    }
                });
            },
            //提交新增的菜单
            handleAdd() {
                //校验
                this.$refs['dataAddForm'].validate((valid) => {
                    if (valid) {
                        //发送ajax请求，将表单数据提交到后台进行处理
                        if (this.formData.BanPath === undefined) {
                            this.formData.BanPath = "";
                        }
                        this.formData.path = this.formData.BanPath + this.formData.path;
                        axios.post("/menu/add.do", this.formData).then((res) => {
                            this.dialogFormVisible = false;
                            if (res.data.flag) {
                                this.$message.success(res.data.message);
                            } else {
                                this.$message.error(res.data.message);
                            }
                        }).catch((res) => {
                            this.showMessage(res);
                        }).finally(() => {
                            this.findPage();
                        });
                    } else {
                        this.$message.error("数据校验失败，请检查你的输入信息是否正确!");
                        return false;
                    }
                });
            },
            //弹出编辑窗口并回显
            handleUpdate(row) {
                this.resetForm();
                this.dialogFormVisible4Edit = true;
                //回显数据
                axios.post("/menu/findById.do?id=" + row.id).then((res) => {
                    if (res.data.flag) {
                        this.formData = res.data.data;
                        //将路由路径分割
                        if (this.formData.path.indexOf("-") != -1) {
                            var str = this.formData.path.split("-");
                            this.formData.BanPath = str[0] + "-";
                            this.formData.path = str[1];
                        } else {
                            this.formData.BanPath = "";
                        }
                        //还需要查询父菜单信息
                        axios.get("/menu/findAllParent.do").then(res => {
                            if (res.data.flag) {
                                this.parentMenu = res.data.data;
                            } else {
                                this.$message.error(res.data.message);
                            }
                        });
                    } else {
                        this.$message.error(res.data.message);
                    }
                }).catch((res) => {
                    this.showMessage(res);
                });
            },
            // 编辑用户
            handleEdit() {
                this.$refs["formData"].validate((valid) => {
                    if (valid) {
                        //点击确定按钮就要提交表单数据
                        if (this.formData.BanPath === undefined) {
                            this.formData.BanPath = "";
                        }
                        this.formData.path = this.formData.BanPath + this.formData.path;
                        axios.post('/menu/edit.do', this.formData).then(res => {
                            this.$message({
                                message: res.data.message,
                                type: res.data.flag ? "success" : "error"
                            });
                            if (res.data.flag) {
                                //说明编辑成功, 要重新获取信息, 不需要重新加载页面
                                this.findPage();
                                //还要关闭编辑权限的窗口
                                this.dialogFormVisible4Edit = false;
                            }
                        }).catch((res) => {
                            this.showMessage(res);
                        })
                    } else {
                        this.$message.error("数据校验失败，请检查你的输入信息是否正确!");
                        return false;
                    }
                });
            },
            // 删除
            handleDelete(row) {
                this.$confirm('此操作将【永久删除】该菜单, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    axios.get("/menu/delete.do?id=" + row.id).then((res) => {
                        this.$message({
                            message: res.data.message,
                            type: res.data.flag ? "success" : "error"
                        });
                        if (res.data.flag) {
                            this.findPage();
                        }
                    }).catch((res) => {
                        this.showMessage(res);
                    });
                }).catch(() => {//点击取消 - 执行
                    this.$message({
                        type: 'info',
                        message: '操作已取消'
                    });
                });
            },
        }
    })
</script>
</html>